//
//  WCInformationDetailVC.m
//  WC
//
//  Created by wsj2015 on 16/1/24.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCInformationDetailVC.h"
#import "WCInforDetailCell.h"
#import "WCUpdateRecordVC.h"
#import "WCQuestionnaireVC.h"
#import "WCPatientInforDetailModel.h"

#import "NSString+Utilities.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WCSendNotifyMessageVC.h"
@interface WCInformationDetailVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headBgView;
@property (weak, nonatomic) IBOutlet UILabel *patientLabel;
@property (weak, nonatomic) IBOutlet UILabel *diagnoseLabel;
@property (weak, nonatomic) IBOutlet UILabel *updateTimeLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, retain) WCPatientInforDetailModel *detailModel;
@property (nonatomic, strong) UIView *footView;
@end

@implementation WCInformationDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self configUI];
    [self postPatientInforDetail];
    
    
}

- (WCPatientInforDetailModel *)detailModel{
    if (_detailModel ==nil) {
        _detailModel =[[WCPatientInforDetailModel alloc] init];
    }
    return _detailModel;
}
- (void)postPatientInforDetail{
//    addDate = 1453874562603;
//    age = 23;
//    isOpen = 0;
//    name = Dddd;
//    patientDisease = Ttttttttttttt;
//    patientId = 207;
//    recordId = 6;
//    recordType = 1;
//    sex = FEMALE;
    weakSelf_define;
    [WCPopupLayerManager showLodingView];
    NSDictionary *params =@{@"recordId":@(self.recordId),@"nurseId":@([[WCDefaultManager shareMg]nurseId])};
    
    [[WCNetworkAccessManager sharedMg] POST:kPatientInforDetail parameters:params success:^(NSDictionary *responseObject) {
        NSDictionary *dictData =responseObject[@"data"];
      
        
        if ([dictData[@"detail"] isKindOfClass:[NSDictionary class]]) {
             [weakSelf.detailModel setValuesForKeysWithDictionary:dictData[@"detail"]];
        }
        
            NSArray *followupList =dictData[@"followupList"];
            NSMutableArray *recordArray =[NSMutableArray array];
            if (followupList && followupList.count>0) {
                for (NSDictionary *dict in followupList) {
                    WCInforRecord *model =[WCInforRecord new];
                    [model setValuesForKeysWithDictionary:dict];
                    [recordArray addObject:model];
                }
                weakSelf.detailModel.inforRecordArray =recordArray;
                
            }
        [weakSelf reloadHeadView];
        [weakSelf.tableView reloadData];
       
        
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
        [weakSelf.tableView.header endRefreshing];
    }];


}

- (void)reloadHeadView{
//    /addDate = 1453874562603;
    //age = 23;
    //isOpen = 0;
    //name = Dddd;
    //patientDisease = Ttttttttttttt;
    //patientId = 207;
    //recordId = 6;
    //recordType = 1;
    //sex = FEMALE;
    
    if (self.detailModel) {
        self.patientLabel.text = [NSString stringWithFormat:@"患者信息:  %@",self.detailModel.name];
        self.diagnoseLabel.text =[NSString stringWithFormat:@"诊断信息:  %@",self.detailModel.patientDisease];
        self.updateTimeLabel.text =[NSString stringWithFormat:@"上传时间:  %@",[NSString timeToString:[self.detailModel.addDate longLongValue]]];
    }
    
    
    

}

- (UIView *)footView{
    if (_footView ==nil) {
        _footView =[[UIView alloc] init];
        _footView.frame =CGRectMake(10, kScreenHeight -30-4 -64, kScreenWidth -20, 30);
        _footView.backgroundColor =[UIColor whiteColor];
        [self.view addSubview:_footView];
        _footView.layer.cornerRadius = 4;
        _footView.layer.borderColor =kBorderColor.CGColor;
        _footView.layer.borderWidth =1;
        _footView.layer.masksToBounds = YES;NSArray *titleArray =@[@"传记录",@"我的问卷",@"发消息"];
        CGFloat space =10;
        CGFloat w =(kScreenWidth - 2*space)/3;
        for (int i = 0; i<3; i++) {
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake(w *i, 0, w, 30);
            
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
            btn.tag = 500 +i;
            [btn addTarget:self action:@selector(btnAcrion:) forControlEvents:UIControlEventTouchUpInside];
            [_footView addSubview:btn];
            
            if (i ==1) {
                UILabel *lline =[[UILabel alloc] initWithFrame:CGRectMake(w +1, 8, 1, 15)];
                [_footView addSubview:lline];
                UILabel *rline =[[UILabel alloc] initWithFrame:CGRectMake(2 * w+2 ,8, 1, 15)];
                [_footView addSubview:rline];
                lline.backgroundColor = kBorderColor;
                rline.backgroundColor =kBorderColor;
            }
            
        }
        
        
    }

    return _footView;
}
#pragma mark -action
- (void)btnAcrion:(UIButton *)btn{
      weakSelf_define;
    if ([[btn currentTitle] isEqualToString:@"传记录"]) {
      
        WCUpdateRecordVC *vc =[[WCUpdateRecordVC alloc] initWithMode:WCUpdateRecordVCModeAdd];
        vc.isAutoRefresh =^(){
            [weakSelf.tableView.header beginRefreshing];
        };
        vc.recordId = [self.detailModel.recordId integerValue];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    if ([[btn currentTitle] isEqualToString:@"我的问卷"]) {
        WCQuestionnaireVC *vc =[WCQuestionnaireVC new];
        vc.recordId =[weakSelf.detailModel.recordId integerValue];
         [weakSelf.navigationController pushViewController:vc animated:YES];
    }
    if ([[btn currentTitle] isEqualToString:@"发消息"]) {
        WCSendNotifyMessageVC *vc =[WCSendNotifyMessageVC new];
        vc.patientId = [self.detailModel.patientId integerValue];
        [self.navigationController pushViewController:vc animated:YES
         ];
    }
    


}
- (void)configUI{
    
//    [self setTitleLabel:@"档案详情" width:100];
//    [self isShowPageBack:YES];
    self.title = @"档案详情";
    self.view.backgroundColor =kAppBg;
    [self.view addSubview:self.footView];
    
    self.headBgView.layer.cornerRadius = 8;
    self.headBgView.layer.borderWidth = 1;
    self.headBgView.layer.borderColor =kBorderColor.CGColor;
    self.headBgView.layer.masksToBounds = YES;
    self.tableView.backgroundColor =kAppBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"WCInforDetailCell" bundle:nil] forCellReuseIdentifier:@"WCInforDetailCellId"];
    weakSelf_define;
   [self.tableView addLegendHeaderWithRefreshingBlock:^{
       [weakSelf postPatientInforDetail];
   }];
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.detailModel) {
        return 0;
    }
    return self.detailModel.inforRecordArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
//    WCPatientInforDetailModel *model = self.detailModel;
    WCInforDetailCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCInforDetailCellId" forIndexPath:indexPath];
    cell.model = self.detailModel.inforRecordArray[indexPath.row];
    weakSelf_define;
    cell.targetVC =^(){
        return weakSelf;
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    weakSelf_define;
    WCUpdateRecordVC *vc =[[WCUpdateRecordVC alloc] initWithMode:WCUpdateRecordVCModeUpdate];
    vc.isAutoRefresh =^(){
        [weakSelf.tableView.header beginRefreshing];
    };
    vc.recoredModel =  self.detailModel.inforRecordArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [tableView fd_heightForCellWithIdentifier:@"WCInforDetailCellId" configuration:^(WCInforDetailCell * cell) {
        cell.model =self.detailModel.inforRecordArray[indexPath.row];
    }];
    

}
@end
